﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace CounterStrikeSourceAimbot
{
    public static class MathUtil
    {
        public static string IntToHex(int value)
        {
            return String.Format("0x{0:X}", value);
        }

        public static int HextToInt(string value)
        {
            if (value.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
            {
                value = value.Substring(2);
            }

            return Int32.Parse(value, NumberStyles.HexNumber);
        }

        public static double[] Vectoangles(double value1, double value2, double value3)
        {
            float forward;
            double yaw, pitch;

            var angles = new double[3];

            if (value1 == 0 && value2 == 0)
            {
                yaw = 0;
                if (value3 > 0) pitch = 90.00;
                else pitch = 270.00;
            }
            else
            {
                if (value1 != -1) yaw = (float)(Math.Atan2(value2, value1) * 180.00 / Math.PI);
                else if (value2 > 0) yaw = 90.00;
                else yaw = 270;
                if (yaw < 0) yaw += 360.00;

                forward = (float)Math.Sqrt((double)(value1 * value1 + value2 * value2));
                pitch = (float)(Math.Atan2((double)value3, (double)forward) * 180.00 / Math.PI);
                if (pitch < 0) pitch += 360.00;
            }

            angles[0] = -pitch;
            angles[1] = yaw;
            angles[2] = 0;

            return angles;
        }

        public static double[] GetPitchYaw(params double[] vec)
        {
            var x = vec[0];
            var y = vec[1];
            var z = vec[2];

            var lxy = Math.Sqrt(x*x + y*y);

            var l = Math.Sqrt(x*x + y*y + z*z);

            var yaw = Math.Atan2(y, x) * 180 / Math.PI;

            var pitch = Math.Asin(-z/l)*180/Math.PI;

            return new double[] {yaw, pitch};
        }


        public static double GetLanda(double deltaYa, double deltaPich)
        {
            var landa = 2 *
                            Math.Asin(Math.Pow(Math.Sin(deltaYa / 2 * Math.PI / 180), 2) +
                                      Math.Pow(Math.Sin(deltaPich / 2 * Math.PI / 180), 2));

            return Math.Sqrt(deltaYa*deltaYa + deltaPich*deltaPich);
            return landa*180/Math.PI;
        }

        public static Tuple<double, double>
            GetAlphaBeta(double val1OnBitmap, double val1OnWorld, double val2OnBitmap, double val2OnWorld)
        {
            var v1 = val1OnWorld;
            var v2 = val2OnWorld;

            var v1p = val1OnBitmap;
            var v2p = val2OnBitmap;

            var alfa = v1p/(v1 - v2) - v2p/(v1 - v2);
            var beta = (v1*v2p)/(v1 - v2) - (v1p*v2)/(v1 - v2);

            return new Tuple<double, double>(alfa, beta);
        }
    }

}
